From df0fc046af067a0531b584f36d1c794ee90b5adc Mon Sep 17 00:00:00 2001 From: "emellor@ewan" Date: Wed, 21 Sep 2005 11:29:23 +0100 Subject: [PATCH] Added diagnostic messages to the RuntimeError exceptions when they occur inside _read and _write. Fix gather to not return the string 'None' when reading a value of type string that does not exist. The value None should be returned instead. Signed-off-by: Ewan Mellor --- tools/python/xen/xend/xenstore/xstransact.py | 29 ++++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/tools/python/xen/xend/xenstore/xstransact.py b/tools/python/xen/xend/xenstore/xstransact.py index 1794f8ab02..170ab60b55 100644 --- a/tools/python/xen/xend/xenstore/xstransact.py +++ b/tools/python/xen/xend/xenstore/xstransact.py @@ -41,7 +41,11 @@ class xstransact: def _read(self, key): path = "%s/%s" % (self.path, key) - return xshandle().read(path) + try: + return xshandle().read(path) + except RuntimeError, ex: + raise RuntimeError(ex.args[0], + '%s, while reading %s' % (ex.args[1], path)) def read(self, *args): if len(args) == 0: @@ -55,7 +59,12 @@ class xstransact: def _write(self, key, data): path = "%s/%s" % (self.path, key) - xshandle().write(path, data) + try: + xshandle().write(path, data) + except RuntimeError, ex: + raise RuntimeError(ex.args[0], + ('%s, while writing %s : %s' % + (ex.args[1], path, str(data)))) def write(self, *args, **opts): if len(args) == 0: @@ -121,10 +130,20 @@ class xstransact: defval = None else: (key, fn, defval) = tup - try: - val = fn(self._read(key)) - except TypeError: + + val = self._read(key) + # If fn is str, then this will successfully convert None to + # 'None'. If it is int, then it will throw TypeError on None, or + # on any other non-integer value. We have to, therefore, both + # check explicitly for None, and catch TypeError. Either failure + # will result in defval being used instead. + if val is None: val = defval + else: + try: + val = fn(val) + except TypeError: + val = defval ret.append(val) if len(ret) == 1: return ret[0] -- 2.30.2